package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.pojo.ClueTable;
import com.neuedu.pojo.Result;
import com.neuedu.service.ClueTableService;
import com.neuedu.mapper.ClueTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【clue_table(线索信息表)】的数据库操作Service实现
* @createDate 2025-06-17 09:04:08
*/
@Service
public class ClueTableServiceImpl extends ServiceImpl<ClueTableMapper, ClueTable>
    implements ClueTableService{
    @Autowired
    private ClueTableMapper clueTableMapper;

    @Override
    public Result<Void> addClueTable(ClueTable clueTable) {
        clueTable.setIsDelete(0);
        clueTable.setCreateTime(new Date());
        clueTable.setUpdateTime(new Date());
        // 添加
        int insert = this.clueTableMapper.insert(clueTable);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<List<ClueTable>> getClueTables(Integer currentPage, Integer pageSize, Integer responsibleCadreid) {
        Page<ClueTable> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<ClueTable> wrapper = new LambdaQueryWrapper<>();
        // 新增条件：如果 responsibleCadreid 不为空，才拼接 eq 条件
        if (responsibleCadreid != null) {
            wrapper.eq(ClueTable::getResponsibleCadreid, responsibleCadreid);
        }
        this.clueTableMapper.selectPage(page, wrapper);
        if (page.getRecords().size() > 0) {
            return Result.success(page.getRecords(), page.getTotal());
        }
        return Result.fail();
    }

    @Override
    public Result<Void> deleteClueTable(Integer id) {
        LambdaUpdateWrapper<ClueTable> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(ClueTable::getIsDelete,1)
                .eq(ClueTable::getClueId,id);
        int update = this.clueTableMapper.update(wrapper);
        if (update > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> updateClueTable(ClueTable clueTable) {
        clueTable.setUpdateTime(new Date());
        // 添加
        int insert = this.clueTableMapper.updateById(clueTable);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> assignClueTable(Integer clueId, Integer cadreId) {
        if (clueId == null || clueId <= 0) {
            return Result.fail("线索ID不合法");
        }
        if (cadreId == null || cadreId <= 0) {
            return Result.fail("干部ID不合法");
        }
        LambdaUpdateWrapper<ClueTable> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(ClueTable::getResponsibleCadreid,cadreId)
                .set(ClueTable::getUpdateTime,new Date())
                .eq(ClueTable::getClueId,clueId);
        int update = this.clueTableMapper.update(wrapper);
        if (update > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void>updateHelpGuidance(Integer clueId,String helpGuidance){
        LambdaUpdateWrapper<ClueTable>wrapper=new LambdaUpdateWrapper<>();
        wrapper.set(ClueTable::getUpdateTime,new Date())
                .set(ClueTable::getHelpGuidance,helpGuidance)
                .eq(ClueTable::getClueId,clueId);
        int update=this.clueTableMapper.update(wrapper);
        if(update>0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<List<ClueTable>>getClueTablesByProvince(Integer currentPage,Integer pageSize,String province){
        Page<ClueTable> page = new Page<>(currentPage,pageSize);
        // 设置条件查询
        LambdaQueryWrapper<ClueTable> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(province), ClueTable::getProvince,province);
        // 查询
        this.clueTableMapper.selectPage(page,wrapper);
        if (page.getRecords().size()>0){
            return Result.success(page.getRecords(),page.getTotal());
        }
        return Result.fail();
    }

    @Override
    public Result<List<ClueTable>>getClueTablesByArea(Integer currentPage, Integer pageSize, String belongArea){
        Page<ClueTable> page = new Page<>(currentPage,pageSize);
        // 设置条件查询
        LambdaQueryWrapper<ClueTable> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(belongArea), ClueTable::getBelongArea,belongArea);
        // 查询
        this.clueTableMapper.selectPage(page,wrapper);
        if (page.getRecords().size()>0){
            return Result.success(page.getRecords(),page.getTotal());
        }
        return Result.fail();
    }


    @Override
    public Result<Void>updateCurrentStage(Integer clueId,String currentStage){
        LambdaUpdateWrapper<ClueTable>wrapper=new LambdaUpdateWrapper<>();
        wrapper.set(ClueTable::getUpdateTime,new Date())
                .set(ClueTable::getCurrentStage,currentStage)
                .eq(ClueTable::getClueId,clueId);
        int update=this.clueTableMapper.update(wrapper);
        if(update>0){
            return Result.success();
        }
        return Result.fail();
    }
}




